<?php

namespace app\models\pay;

use Yii;

/**
 * This is the model class for table "pay_for".
 *
 * @property int $id
 * @property string $sn 订单传过来的SN, 如果订单SN修改,这里会重新创建一条记录
 * @property int $user_id
 * @property float $amount 代付金额
 * @property int $status 0未完成 1已完成
 * @property int $type 类型, 1 提现 2 付给商家
 * @property string $type_text 类型文本说明
 * @property string $create_time
 * @property string $update_time
 * @property string $request_params
 * @property string $bank_number
 * @property string $bank_nickname
 */
class PayFor extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'pay_for';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'status', 'type'], 'integer'],
            [['amount'], 'number'],
            [['create_time', 'update_time'], 'safe'],
            [['sn', 'type_text'], 'string', 'max' => 255],
            [['sn'], 'unique'],

            // json
            [['request_params'], 'safe'],

            [['bank_number', 'bank_nickname'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'sn' => 'Sn',
            'user_id' => 'User ID',
            'amount' => 'Amount',
            'status' => 'Status',
            'type' => 'Type',
            'type_text' => 'Type Text',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }
}
